package test;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.Chapter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * @author xgy
 * @date 2020-09-12 19:16
 */

public class MyThread implements Runnable {
    public static ArrayList<Chapter> zhangJieNeiRong = new ArrayList<>();
    public static AtomicInteger ai = new AtomicInteger();
    public static Object o =new Object();
    @Override
    public void run() {

        Element muLu = null;
        try {
            muLu = Jsoup.connect("https://hualsy.com/book/1238.html").timeout(0).get().select("dl.panel-body.panel-chapterlist").get(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Elements elements = muLu.select(".col-md-3");
        for (ai.get(); ai.get() < elements.size(); ) {
            int id = ai.addAndGet(1);
            ArrayList<Object> list;
            String href;
            href = elements.get(ai.get() -1 ).select("a").attr("href");
            list = null;
            for (int i1 = 0; i1 < 10; i1++) {
                try {
                    list = paChuYiZhang(href);
                    break;
                } catch (Exception e) {
                    if(i1 == 9){
                        BufferedWriter bufferedWriter = null;
                        try {
                            bufferedWriter = new BufferedWriter(new FileWriter("reptileDemozcy/books/log/error/CuoWu.txt", true));
                            bufferedWriter.write((ai.get()+1)+"章出现了问题,没有获取到");
                            bufferedWriter.newLine();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }finally {
                            try {
                                bufferedWriter.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }
            }
                String biaoTi =(String) list.get(0);
                File neiRong = (File) list.get(1);
                String url = (String) list.get(2);
                Chapter chapter = new Chapter(id, biaoTi, neiRong, url);
                zhangJieNeiRong.add(chapter);

        }
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("reptileDemozcy/books/ZhangJie.txt",false));
            String s = JSON.toJSONString(zhangJieNeiRong);
            bw.write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private ArrayList paChuYiZhang(String src) {
        int b =ai.get();
        String url = "https://hualsy.com" + src;
        Document Zhang = null;
        try {
            Zhang = Jsoup.connect("https://hualsy.com" + src).timeout(0).get();
        } catch (IOException e) {

        }
        String neiRong = Zhang.select("div#htmlContent.panel-body").get(0).text();
        String biaoTi = Zhang.select("h1.readTitle").get(0).text();
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("reptileDemozcy/books/773/"+ b+"_"+biaoTi+".txt"));
            bw.write(neiRong);
        } catch (Exception e) {
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("reptileDemozcy/books/log/error/CuoWu.txt", true));
                bufferedWriter.write(b+"章标题出现了问题,没有获取到");
                bufferedWriter.newLine();
                bufferedWriter.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        File file = new File("reptileDemozcy/books/773/" + b + "_" + biaoTi + ".txt");
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(biaoTi);
        objects.add(file);
        objects.add(url);
        return objects;
    }
}
